"""
循环队列
"""
from threading import Lock


class Requeue:
    def __init__(self, maxsize):
        self.queue = [None] * maxsize
        self.maxsize = maxsize
        self.front = 0
        self.rear = 0
        self.lock = Lock()

    def push(self, value):
        with self.lock:
            if (self.rear + 1) % self.maxsize == self.front:
                print("队列已满")
            else:
                self.queue[self.rear] = value
                self.rear = (self.rear + 1) % self.maxsize

    def pop(self):
        with self.lock:
            if self.rear == self.front:
                print("队列已空")
            else:
                out = self.queue[self.front]
                self.queue[self.front] = None
                self.front = (self.front + 1) % self.maxsize
                return out

    def isEmpty(self):
        if self.rear == self.front:
            return True
        return False

    def isFull(self):
        if (self.rear + 1) % self.maxsize == self.front:
            return True
        return False

    def traverse(self):
        return self.queue

    def show(self):
        alist = []
        if self.front == self.rear:
            return alist
        if self.front < self.rear:
            for i in range(self.front, self.rear + 1):
                if self.queue[i] is not None:
                    alist.append(self.queue[i])
        else:
            for i in list(range(self.front, self.maxsize)) + list(range(0, self.rear + 1)):
                if self.queue[i] is not None:
                    alist.append(self.queue[i])
        return alist
